<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers\Example;

use App\Filters\Example\ExampleTreeTableFilter;
use App\Http\Controllers\Controller;
use App\Http\Requests\Example\ExampleTreeTableRequest;
use App\Http\Resources\Example\ExampleTreeTableCollection;
use App\Http\Resources\Example\ExampleTreeTableResource;
use App\Models\Example\ExampleTreeTable;
use App\Services\Example\ExampleTreeTableService;
use App\Services\TreeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 树形表格演示模块
 * Class ExampleTreeTableController
 * @package App\Http\Controllers
 */
class ExampleTreeTableController extends Controller
{

    /**
     * 列表
     * @param ExampleTreeTableFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(ExampleTreeTableFilter $filter)
    {
        $filter = ExampleTreeTable::filter($filter)->get();
        $collection = ExampleTreeTableCollection::make($filter)->toArray(null);
        $items = TreeService::make($collection['items']);
        return $this->success('', ['items' => $items, 'total' => count($collection['items'])]);
    }

    /**
     * 添加
     * @param ExampleTreeTableRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(ExampleTreeTableRequest $request)
    {
        $data = $request->validate('store');
        $example = new ExampleTreeTable($data);
        if (!$example->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param ExampleTreeTable $example_tree_table
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ExampleTreeTable $example_tree_table)
    {
        $exampleTreeTableResource = ExampleTreeTableResource::make($example_tree_table)->except(['created_at', 'updated_at', 'sort'])->toArray(null);
        return $this->success('', $exampleTreeTableResource);
    }

    /**
     * 更新
     * @param ExampleTreeTableRequest $request
     * @param ExampleTreeTable $example_tree_table
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(ExampleTreeTableRequest $request, ExampleTreeTable $example_tree_table)
    {
        $data = $request->validate('update');
        if (!$example_tree_table->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param ExampleTreeTable $example_tree_table
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(ExampleTreeTable $example_tree_table)
    {
        DB::beginTransaction();
        try {
            $sub = ExampleTreeTable::where('parent_id', $example_tree_table->id)->first();
            // 存在下级则禁止删除
            if ($sub) {
                return $this->error(__('example/exampleTreeTable.prohibit_delete_with_subs'));
            }
            $example_tree_table->delete();
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param ExampleTreeTableRequest $request
     * @param ExampleTreeTable $example_tree_table
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(ExampleTreeTableRequest $request, ExampleTreeTable $example_tree_table)
    {
        $data = $request->validate('updateStatus');
        if (!$example_tree_table->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 更新排序
     * @param ExampleTreeTableRequest $request
     * @param ExampleTreeTable $example_tree_table
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateSort(ExampleTreeTableRequest $request, ExampleTreeTable $example_tree_table)
    {
        $data = $request->validate('updateSort');
        if (!$example_tree_table->batchUpdate($data['sort'])) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 上一级选择
     * @param $id // 当前id
     * @return \Illuminate\Http\JsonResponse
     */
    public function parentTreeConfig($id)
    {
        return $this->success('', ExampleTreeTableService::getParentTree($id));
    }

    /**
     * 普通表格模块所需分类
     * @return \Illuminate\Http\JsonResponse
     */
    public function treeConfig()
    {
        $item = ExampleTreeTable::select('id', 'parent_id', 'name')->orderBy('sort', 'asc')->orderBy('id', 'desc')->get()->toArray();
        $result = TreeService::make($item);
        return $this->success('', ['items' => $result]);
    }

}
